﻿using System;
using UnityEngine;
using UnityEngine.UI;

// Token: 0x0200038E RID: 910
public class RaygunReticule : MonoBehaviour
{
	// Token: 0x060017BA RID: 6074 RVA: 0x00096784 File Offset: 0x00094984
	private void Start()
	{
		this.polarityVal = 0.5f;
		this.firingModeVal = 0;
		this.crosshair = base.transform.GetChild(0).gameObject;
		this.crosshairImage = this.crosshair.GetComponent<Image>();
		this.SetupUI();
	}

	// Token: 0x060017BB RID: 6075 RVA: 0x000967D4 File Offset: 0x000949D4
	public void SetupUI()
	{
		this.SetupCrosshair();
		this.SetupPolarityBar();
		this.SetupFiringModeBar();
		this.auxiliaryUIColor = new Color(GlobalPreferences.AuxiliaryUIColorR.value, GlobalPreferences.AuxiliaryUIColorG.value, GlobalPreferences.AuxiliaryUIColorB.value);
		this.ChangeAuxiliaryColor(this.auxiliaryUIColor);
		this.shouldFadeUI = GlobalPreferences.AuxiliaryFade.value;
	}

	// Token: 0x060017BC RID: 6076 RVA: 0x00096838 File Offset: 0x00094A38
	private void SetupCrosshair()
	{
		string crosshairImageName = this.GetCrosshairImageName();
		this.crosshairTexture = UnityEngine.Object.Instantiate<Texture2D>(Resources.Load<Texture2D>("Raygun/UI/" + crosshairImageName));
		Color[] pixels = this.crosshairTexture.GetPixels();
		Color a = new Color(GlobalPreferences.CrosshairColorR.value, GlobalPreferences.CrosshairColorG.value, GlobalPreferences.CrosshairColorB.value);
		Color color = Color.clear;
		int num = 1;
		switch (GlobalPreferences.CrosshairOutline.value)
		{
		case 0:
			color = Color.clear;
			num = 0;
			break;
		case 1:
			color = Color.black;
			break;
		case 2:
			color = Color.white;
			break;
		case 3:
			color = new Color(1f - a.r, 1f - a.g, 1f - a.b, 1f);
			break;
		case 4:
			color = a * 0.6f;
			break;
		case 5:
			color = a * 1.4f;
			break;
		}
		for (int i = 0; i < pixels.Length; i++)
		{
			if (pixels[i].a > 0f)
			{
				if (pixels[i].r != 0f)
				{
					pixels[i] = new Color(a.r, a.g, a.b, pixels[i].a);
				}
				else
				{
					pixels[i] = new Color(color.r, color.g, color.b, pixels[i].a * (float)num);
				}
			}
		}
		this.crosshairTexture.SetPixels(pixels);
		this.crosshairTexture.Apply();
		this.crosshairImage.sprite = Sprite.Create(this.crosshairTexture, new Rect(0f, 0f, (float)this.crosshairTexture.width, (float)this.crosshairTexture.height), new Vector2(0.5f, 0.5f), 100f);
		this.crosshair.transform.localScale = Vector3.one * GlobalPreferences.UICrosshairScale.value;
	}

	// Token: 0x060017BD RID: 6077 RVA: 0x00096A94 File Offset: 0x00094C94
	private string GetCrosshairImageName()
	{
		switch (GlobalPreferences.CrosshairImage.value)
		{
		case 0:
			return "reticule_0";
		case 1:
			return "reticule_1";
		case 2:
			return "reticule_2";
		case 3:
			return "reticule_3";
		case 4:
			return "reticule_4";
		case 5:
			return "reticule_5";
		case 6:
			return "scifi_reticule_1";
		case 7:
			return "scifi_reticule_2";
		case 8:
			return "arc_reticule_1";
		case 9:
			return "arc_reticule_2";
		default:
			return "reticule_0";
		}
	}

	// Token: 0x060017BE RID: 6078 RVA: 0x00096B20 File Offset: 0x00094D20
	private void SetupPolarityBar()
	{
		if (this.polarityBar != null)
		{
			UnityEngine.Object.Destroy(this.polarityBar);
		}
		float value = GlobalPreferences.UIAuxiliaryScale.value;
		int value2 = GlobalPreferences.PolarityBarLocation.value;
		if (value2 != 0)
		{
			if (value2 != 1)
			{
				if (value2 == 2)
				{
					this.polarityBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/polarity_bar_corner"));
					this.polarityBar.transform.SetParent(base.gameObject.transform);
					RectTransform component = this.polarityBar.GetComponent<RectTransform>();
					component.anchorMin = new Vector2(0f, 0f);
					component.anchorMax = new Vector2(0f, 0f);
					component.anchoredPosition = new Vector2(100f * value, 100f * value);
				}
			}
			else
			{
				this.polarityBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/polarity_bar_corner"));
				this.polarityBar.transform.SetParent(base.gameObject.transform);
				RectTransform component = this.polarityBar.GetComponent<RectTransform>();
				component.anchorMin = new Vector2(1f, 0f);
				component.anchorMax = new Vector2(1f, 0f);
				component.anchoredPosition = new Vector2(-100f * value, 100f * value);
			}
		}
		else
		{
			this.polarityBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/polarity_bar_center"));
			this.polarityBar.transform.SetParent(base.gameObject.transform);
			RectTransform component = this.polarityBar.GetComponent<RectTransform>();
			component.anchoredPosition = new Vector2(0f, -30f * value);
		}
		this.polarityBar.transform.localScale = Vector3.one * GlobalPreferences.UIAuxiliaryScale.value;
		this.polarityBarImage = this.polarityBar.GetComponent<Image>();
		this.polarityBarImage.material.SetFloat("_magnitude_value", this.polarityVal);
		this.polarityBarImage.material.SetFloat("_alpha", 1f);
	}

	// Token: 0x060017BF RID: 6079 RVA: 0x00096D44 File Offset: 0x00094F44
	private void SetupFiringModeBar()
	{
		if (this.firingModeBar != null)
		{
			UnityEngine.Object.Destroy(this.firingModeBar);
		}
		float value = GlobalPreferences.UIAuxiliaryScale.value;
		switch (GlobalPreferences.FiringModeBarLocation.value)
		{
		case 0:
		{
			this.firingModeBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/firing_mode_bar"));
			this.firingModeBar.transform.SetParent(base.gameObject.transform);
			RectTransform component = this.firingModeBar.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.anchoredPosition = new Vector2(0f, 70f * value);
			break;
		}
		case 1:
		{
			this.firingModeBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/firing_mode_bar"));
			this.firingModeBar.transform.SetParent(base.gameObject.transform);
			RectTransform component = this.firingModeBar.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0.5f);
			component.anchorMax = new Vector2(0.5f, 0.5f);
			component.anchoredPosition = new Vector2(0f, (float)((GlobalPreferences.PolarityBarLocation.value != 0) ? -80 : -130) * value);
			break;
		}
		case 2:
		{
			this.firingModeBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/firing_mode_bar"));
			this.firingModeBar.transform.SetParent(base.gameObject.transform);
			RectTransform component = this.firingModeBar.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 0f);
			component.anchorMax = new Vector2(1f, 0f);
			component.anchoredPosition = new Vector2(-100f * value, 30f * value);
			break;
		}
		case 3:
		{
			this.firingModeBar = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Raygun/UI/firing_mode_bar"));
			this.firingModeBar.transform.SetParent(base.gameObject.transform);
			RectTransform component = this.firingModeBar.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0f, 0f);
			component.anchorMax = new Vector2(0f, 0f);
			component.anchoredPosition = new Vector2(100f * value, 30f * value);
			break;
		}
		}
		this.firingModeBar.transform.localScale = Vector3.one * GlobalPreferences.UIAuxiliaryScale.value;
		this.firingModeBarImage = this.firingModeBar.GetComponent<Image>();
		this.firingModeBarImage.material.SetFloat("_mode", (float)this.firingModeVal);
		this.firingModeBarImage.material.SetFloat("_alpha", 1f);
	}

	// Token: 0x060017C0 RID: 6080 RVA: 0x0009702C File Offset: 0x0009522C
	private void Update()
	{
		if (GameController.Instance.paused && !this.isHidden)
		{
			this.HideUI();
		}
		else if (!GameController.Instance.paused && this.isHidden)
		{
			this.UnhideUI();
		}
		if (this.shouldFadeUI)
		{
			this.UpdateFade();
		}
	}

	// Token: 0x060017C1 RID: 6081 RVA: 0x0009708F File Offset: 0x0009528F
	private void HideUI()
	{
		base.GetComponent<Canvas>().enabled = false;
		this.isHidden = true;
	}

	// Token: 0x060017C2 RID: 6082 RVA: 0x000970A4 File Offset: 0x000952A4
	private void UnhideUI()
	{
		base.GetComponent<Canvas>().enabled = true;
		this.isHidden = false;
	}

	// Token: 0x060017C3 RID: 6083 RVA: 0x000970BC File Offset: 0x000952BC
	private void UpdateFade()
	{
		if (this.fadeTimer < 0f)
		{
			this.isFiringModeFaded = this.FadeFiringModeBar();
			this.isPolarityBarFaded = this.FadePolarityBar();
			this.shouldFadeUI = (!this.isFiringModeFaded || !this.isPolarityBarFaded);
		}
		else
		{
			this.fadeTimer -= Time.deltaTime;
		}
	}

	// Token: 0x060017C4 RID: 6084 RVA: 0x00097128 File Offset: 0x00095328
	public void ResetFade()
	{
		this.shouldFadeUI = GlobalPreferences.AuxiliaryFade.value;
		this.fadeTimer = GlobalPreferences.AuxiliaryFadeDelay.value;
		if (!this.isFiringModeFaded || !this.shouldFadeUI)
		{
			this.UnfadeFiringModeBar();
		}
		if (!this.isPolarityBarFaded || !this.shouldFadeUI)
		{
			this.UnfadePolarityBar();
		}
	}

	// Token: 0x060017C5 RID: 6085 RVA: 0x00097190 File Offset: 0x00095390
	public void ChangeAuxiliaryColor(Color color)
	{
		Material material = this.polarityBarImage.material;
		material.SetColor("_UI_Color", color);
		this.polarityBarImage.material = material;
		material = this.firingModeBarImage.material;
		material.SetColor("_UI_Color", color);
		this.firingModeBarImage.material = material;
	}

	// Token: 0x060017C6 RID: 6086 RVA: 0x000971E5 File Offset: 0x000953E5
	public void ChangePolarityValue(float val)
	{
		this.polarityVal = val;
		this.polarityBarImage.material.SetFloat("_magnitude_value", val);
		this.isPolarityBarFaded = false;
		this.ResetFade();
	}

	// Token: 0x060017C7 RID: 6087 RVA: 0x00097211 File Offset: 0x00095411
	public void ChangeFiringMode(int val)
	{
		this.firingModeVal = val;
		this.firingModeBarImage.material.SetFloat("_mode", (float)val);
		this.isFiringModeFaded = false;
		this.ResetFade();
	}

	// Token: 0x060017C8 RID: 6088 RVA: 0x00097240 File Offset: 0x00095440
	private bool FadePolarityBar()
	{
		float num = this.polarityBarImage.material.GetFloat("_alpha");
		if (num == 0f)
		{
			this.isPolarityBarFaded = true;
			return true;
		}
		num = Mathf.Clamp01(num - Time.deltaTime);
		this.polarityBarImage.material.SetFloat("_alpha", num);
		return false;
	}

	// Token: 0x060017C9 RID: 6089 RVA: 0x0009729B File Offset: 0x0009549B
	public void UnfadePolarityBar()
	{
		this.polarityBarImage.material.SetFloat("_alpha", 1f);
	}

	// Token: 0x060017CA RID: 6090 RVA: 0x000972B8 File Offset: 0x000954B8
	private bool FadeFiringModeBar()
	{
		float num = this.firingModeBarImage.material.GetFloat("_alpha");
		if (num == 0f)
		{
			this.isFiringModeFaded = true;
			return true;
		}
		num = Mathf.Clamp01(num - Time.deltaTime);
		this.firingModeBarImage.material.SetFloat("_alpha", num);
		return false;
	}

	// Token: 0x060017CB RID: 6091 RVA: 0x00097313 File Offset: 0x00095513
	public void UnfadeFiringModeBar()
	{
		this.firingModeBarImage.material.SetFloat("_alpha", 1f);
	}

	// Token: 0x040019FC RID: 6652
	private Texture2D crosshairTexture;

	// Token: 0x040019FD RID: 6653
	private GameObject crosshair;

	// Token: 0x040019FE RID: 6654
	private Image crosshairImage;

	// Token: 0x040019FF RID: 6655
	private GameObject polarityBar;

	// Token: 0x04001A00 RID: 6656
	private Image polarityBarImage;

	// Token: 0x04001A01 RID: 6657
	private GameObject firingModeBar;

	// Token: 0x04001A02 RID: 6658
	private Image firingModeBarImage;

	// Token: 0x04001A03 RID: 6659
	private Color auxiliaryUIColor;

	// Token: 0x04001A04 RID: 6660
	private float fadeTimer = 2f;

	// Token: 0x04001A05 RID: 6661
	private bool isFiringModeFaded;

	// Token: 0x04001A06 RID: 6662
	private bool isPolarityBarFaded;

	// Token: 0x04001A07 RID: 6663
	private bool shouldFadeUI;

	// Token: 0x04001A08 RID: 6664
	private float polarityVal;

	// Token: 0x04001A09 RID: 6665
	private int firingModeVal;

	// Token: 0x04001A0A RID: 6666
	private bool isHidden;
}
